<!--
 * @Description: 发货信息
 * 
 * @Author: XiongYaoYuan<xiongyaoyuan>
 * @Date: 2023-07-31 22:23:35
 * @FilePath: /yuxiuproweb/src/views/mes/pro/workorder/mods/room/delivery/logistics.vue
-->
<template>
  <el-dialog
    :close-on-click-modal="false"
    v-dialogDrag
    title="发货信息"
    :visible.sync="shows"
    width="700px"
    append-to-body
  >
    <div class="mb20">
      <el-radio-group v-model="paymentMethod" @change="getList">
        <el-radio label="0">寄付</el-radio>
        <el-radio label="1">到付</el-radio>
        <el-radio label="2">包邮</el-radio>
      </el-radio-group>
    </div>
    <div class="mb10" v-if="companyRow">
      已选择：{{ companyRow.name }}({{
        companyRow.paymentMethod === "0"
          ? "寄付"
          : companyRow.paymentMethod === "1"
          ? "到付"
          : companyRow.paymentMethod === "2"
          ? "包邮"
          : ""
      }})
    </div>
    <el-table
      ref="multipleTable"
      :data="companyList"
      @row-click="handleRowClick"
    >
      <el-table-column width="60" align="center">
        <template slot-scope="{ row }">
          <el-radio
            v-model="row.checked"
            class="radio-view"
            :label="true"
            @change="checkedModelChange(row)"
            ><i></i
          ></el-radio>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="支付方式" align="center" prop="paymentMethod">
        <template slot-scope="{ row }">
          {{
            row.paymentMethod === "0"
              ? "寄付"
              : row.paymentMethod === "1"
              ? "到付"
              : row.paymentMethod === "2"
              ? "包邮"
              : ""
          }}
        </template>
      </el-table-column>
      <el-table-column label="信息" align="center" prop="info" />
      <el-table-column label="限长" align="center" prop="limitLength" />
      <el-table-column label="邮费" align="center" prop="postage" />
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="shows = false">取 消</el-button>
      <el-button type="warning" @click="submitClean">清 除</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { listCompany } from "@/api/transport/logistics";
export default {
  props: {
    show: Boolean,
    data: {
      type: Object,
    },
  },
  data() {
    return {
      companyList: [],
      paymentMethod: "0",
      companyRow: null,
    };
  },
  computed: {
    shows: {
      get() {
        return this.show;
      },
      set(newValue) {
        this.$emit("update:show", newValue);
      },
    },
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      listCompany({
        pageNum: 1,
        pageSize: 100,
        paymentMethod: this.paymentMethod === "1" ? this.paymentMethod : "",
      }).then(({ rows }) => {
        // this.companyRow = null;
        this.companyList = (rows || []).map((e, index) => {
          return {
            ...e,
            subIndex: index,
            checked:
              this.companyRow?.logisticsCompanyId === e.logisticsCompanyId,
          };
        });
      });
    },
    checkedModelChange(row) {
      this.companyList?.forEach((e) => {
        if (e.subIndex !== row.subIndex) e.checked = false;
        else e.checked = true;
      });
      this.companyRow = this.companyList?.find((e) => e.checked);
    },
    handleRowClick(row) {
      this.checkedModelChange(row);
    },
    // 保存
    submitForm() {
      if (!this.companyRow) {
        this.$message.error("请选择快递");
        return;
      }
      this.$emit("on-change", {
        ...this.companyRow,
        paymentMethod: this.paymentMethod,
      });
      this.shows = false;
    },
    // 清除
    submitClean() {
      this.$emit("on-clean");
      this.shows = false;
    },
  },
};
</script>
